package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.CategorySellAmts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qf.mapper.StatMapper;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DtsCategoryServiceImpl implements DtsCategoryService {
    @Autowired
    DtsCategoryMapper categoryMapper;


    @Autowired
    StatMapper statMapper;

    @Override
    public List<CategorySellAmts> findCategorySellData() {

        List<CategorySellAmts> list = statMapper.findCategorySellData();
        return list;
    }

    @Override
    public List<CatVo> findList() {
        LambdaQueryWrapper<DtsCategory> qw= new LambdaQueryWrapper<>();
        qw.eq(DtsCategory::getPid, 0);
        qw.eq(DtsCategory::getDeleted, false);
        List<DtsCategory> categories = categoryMapper.selectList(qw);
        List<CatVo> catVos = categories.stream().map(cat -> {

            CatVo catVo = new CatVo();
            catVo.setLabel(cat.getName());
            catVo.setValue(cat.getId());

            LambdaQueryWrapper<DtsCategory> qw1 = new LambdaQueryWrapper<>();
            qw1.eq(DtsCategory::getPid, cat.getId());
            qw1.eq(DtsCategory::getDeleted, false);
            List<DtsCategory> list = categoryMapper.selectList(qw1);
            List<CatVo> catvo2 = list.stream().map(cat2 -> {

                CatVo catVo2 = new CatVo();
                catVo2.setValue(cat2.getId());
                catVo2.setLabel(cat2.getName());

                return catVo2;
            }).collect(Collectors.toList());

            catVo.setChildren(catvo2);

            return catVo;
        }).collect(Collectors.toList());

        return catVos;
    }

    @Override
    public DtsCategory findOne(Integer id1) {
        return categoryMapper.selectById(id1);
    }

    @Override
    public IPage<DtsCategory> queryPage(Integer page, Integer limit, String sort, String order, String name, Integer id) {
        IPage<DtsCategory> ipage = new Page(page, limit);
        QueryWrapper<DtsCategory> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)) {
            qw.like("username", name);
        }
        if(!StringUtils.isEmpty(id)) {
            qw.like("id", id);
        }
//        qw.eq("deleted",0);
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if(order.equalsIgnoreCase("desc")) {
                qw.orderByDesc(sort);
            } else {
                qw.orderByAsc(sort);
            }
        }
        categoryMapper.selectPage(ipage,qw);

        return ipage;
    }

    @Override
    public List<DtsCategory> find() {
        LambdaQueryWrapper<DtsCategory> qw=new LambdaQueryWrapper<>();
        qw.eq(DtsCategory::getPid,0);
        List<DtsCategory> dtsCategories = categoryMapper.selectList(qw
        );
        return dtsCategories;
    }

    @Override
    public DtsCategory create(DtsCategory category) {
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
//        if("l1".equalsIgnoreCase(category.getLevel())){
//            category.setPid(0);
//        }
        categoryMapper.insert(category);
        return category;
    }

    @Override
    public DtsCategory update(DtsCategory category) {
        category.setUpdateTime(new Date());
        LambdaQueryWrapper<DtsCategory> qw=new LambdaQueryWrapper<>();
        qw.eq(DtsCategory::getName,category.getName());
        Integer integer = categoryMapper.selectCount(qw);
        if(integer>0){
            ResponseUtil.fail(-1,"品牌名已存在");
        }
         categoryMapper.updateById(category);
        return category;
    }
}
